local skel = fk.CreateSkill {
  name = "lb_zero__diaoye",
  tags = { Skill.Compulsory },
  max_branches_use_time = function(self, player)
    local list = {
      ["turn"] = {
        [Player.HistoryTurn] = 1
      },
      ["round"] = {
        [Player.HistoryRound] = 1
      },
    }
    --[[
    local room = Fk:currentRoom()
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryRound] = 1 }
    end
    --]]
    return list
  end
}

Fk:loadTranslationTable {
  ["lb_zero__diaoye"] = "凋曳",
  --[":lb_zero__diaoye"] = "<b>锁定技</b>，你的手牌上限+X（X为你本局游戏受到的伤害数）。每回合限一次，当你受到伤害后，你摸X张牌，若<a href=':lb_zero__shanglei'><font color='red'>〖殇蕾〗</font></a>目标已受伤，你视为对其以外的一名角色使用一张刺【杀】。",
  --10.31 光速被砍
  --[":lb_zero__diaoye"] = "<b>锁定技</b>，你每轮首次受伤后摸X张牌。你的手牌上限+X（X为你本局游戏受到的伤害数）。每回合限一次，当你受到伤害后，若<a href=':lb_zero__shanglei'><font color='red'>〖殇蕾〗</font></a>目标已受伤，你视为对其以外的一名角色使用一张刺【杀】。",
  --11.18
  --[":lb_zero__diaoye"] = "<b>锁定技</b>，每轮每名角色限一次，当你受到一名角色的伤害后，你摸X张牌。你的手牌上限+X（X为你本局游戏受到的伤害数，至多为“殇蕾”目标的体力上限）。" ..
      --"每回合限一次，当你受到伤害后，若<a href=':lb_zero__shanglei'><font color='red'>〖殇蕾〗</font></a>目标已受伤，你视为对其以外的一名角色使用一张刺【杀】。",
  --11.20
    [":lb_zero__diaoye"] = "<b>锁定技</b>，每轮限一次，当你受到伤害后，你摸X张牌。你的手牌上限+X（X为你本局游戏受到的伤害数，至多为“殇蕾”目标的体力上限）。" ..
      "每回合限一次，当你受到伤害后，若<a href=':lb_zero__shanglei'><font color='red'>〖殇蕾〗</font></a>目标已受伤，你视为对其以外的一名角色使用一张刺【杀】。",

  ["@lb_zero__diaoye"] = "<font color='red'>凋曳</font>",
  ["@[skill_players]lb_zero__diaoye"] = "凋曳",

  ["#lb_zero__diaoye"] = "凋曳：视为对除%src外的一名角色使用刺【杀】",

  ["$lb_zero__diaoye1"] = "别想弄脏我的裙子。",
  ["$lb_zero__diaoye2"] = "不要耽误我和法厄同大人的时间。",
  ["$lb_zero__diaoye3"] = "蠢东西。",
  ["$lb_zero__diaoye4"] = "有脏东西。",
}

---@param player ServerPlayer
local diaoye__slash = function(player)
  local room = player.room
  local to = player:getMark("@lb_zero__shanglei")
  if player:isAlive() and to ~= 0 and to:isAlive() and to:isWounded() then
    return room:askToUseVirtualCard(player, {
      name = "stab__slash",
      skill_name = skel.name,
      cancelable = false,
      prompt = "#lb_zero__diaoye:" .. to.id,
      extra_data = { exclusive_targets = table.map(room:getOtherPlayers(to), Util.IdMapper) },
      skip = true,
    })
  end
  return nil
end

skel:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and
        (
          player:usedSkillTimes(skel.name, Player.HistoryTurn, "turn") == 0 
          or player:usedSkillTimes(skel.name, Player.HistoryRound, "round") == 0 
          --(data.from and skel:withinBranchTimesLimit(player, tostring(data.from.id), Player.HistoryRound))
        )
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    --[[
    local damage_events = room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
      local damage = e.data
      return damage.to == player and not damage.prevented
    end, Player.HistoryRound)
    if damage_events[1] and damage_events[1].data == data then
      event:setCostData(self, { num = player:getMark("@lb_zero__diaoye") })
      room:useSkill(player, self, function()
        self:use(event, target, player, data)
      end, event.skill_data)
    else
      local use = diaoye__slash(player)
      if use then
        event:setCostData(self, { use = use })
        room:useSkill(player, self, function()
          self:use(event, target, player, data)
        end, event.skill_data)
      end
    end
    --]]
    if data.from and skel:withinBranchTimesLimit(player, "round", Player.HistoryRound) then
      event:setCostData(self, { draw = true })
      room:useSkill(player, self, function()
        self:use(event, target, player, data)
      end, event.skill_data)
    end
    if player:usedSkillTimes(skel.name, Player.HistoryTurn, "turn") == 0 then
      local use = diaoye__slash(player)
      if use then
        event:setCostData(self, { use = use })
        room:useSkill(player, self, function()
          self:use(event, target, player, data)
        end, event.skill_data)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.draw then
      --player:addSkillBranchUseHistory(skel.name, tostring(data.from.id))
      player:addSkillBranchUseHistory(skel.name, "round")
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:broadcastSkillInvoke(skel.name, math.random(1, 2))
      local to = player:getMark("@lb_zero__shanglei")
      local num = player:getMark("@lb_zero__diaoye")
      if to ~= 0 and to:isAlive() then
        num = math.max(0, math.min(to.maxHp, num))
      end
      player:drawCards(num, skel.name)
    else
      local use = cost_data.use
      if use then
        player:addSkillBranchUseHistory(skel.name, "turn")
        room:notifySkillInvoked(player, skel.name, "offensive")
        player:broadcastSkillInvoke(skel.name, math.random(3, 4))
        room:useCard(use)
      end
    end
  end,
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    skel:onAcquire(player, false)
  end
})

skel:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(skel.name) then
      local to = player:getMark("@lb_zero__shanglei")
      local num = player:getMark("@lb_zero__diaoye")
      if to ~= 0 and to:isAlive() then
        return math.max(0, math.min(to.maxHp, num))
      end
      return num
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local damage = 0
  room.logic:getEventsOfScope(GameEvent.Damage, 99999, function(e)
    if e.data.to == player and not e.data.prevented then
      damage = damage + e.data.damage
    end
  end, Player.HistoryGame)
  local to = player:getMark("@lb_zero__shanglei")
  if to ~= 0 and to:isAlive() then
    damage = math.max(0, math.min(to.maxHp, damage))
  end
  
  player.room:setPlayerMark(player, "@lb_zero__diaoye", damage)
  --[[
    player.room:setPlayerMark(player, "@[skill_players]lb_zero__diaoye",
    { name = skel.name, scope = Player.HistoryRound, players = table.map(player.room.players, Util.IdMapper) })
     --]]
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb_zero__diaoye", 0)
    --player.room:setPlayerMark(player, "@[skill_players]lb_zero__diaoye", 0)
end)

return skel
